# @Version        : 1.0
# @Update Time    : 2025/8/29 1:46
# @File           : config.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
from dataclasses import dataclass
from typing import Optional, Dict, Any


@dataclass
class RedisConfig:
    """Redis 配置类 - 参考 Sa-Token 的配置方式"""

    # 基础连接配置
    host: str = "localhost"
    port: int = 6379
    db: int = 0
    password: Optional[str] = None

    # 连接池配置
    max_connections: int = 20
    socket_connect_timeout: int = 5
    socket_timeout: int = 5
    socket_keepalive: bool = True
    retry_on_timeout: bool = True

    # 序列化配置
    decode_responses: bool = True

    # 集群配置
    cluster_mode: bool = False
    cluster_nodes: Optional[list] = None

    # 哨兵配置
    sentinel_mode: bool = False
    sentinel_hosts: Optional[list] = None
    sentinel_service_name: Optional[str] = None

    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        config = {
            "host": self.host,
            "port": self.port,
            "db": self.db,
            "max_connections": self.max_connections,
            "socket_connect_timeout": self.socket_connect_timeout,
            "socket_timeout": self.socket_timeout,
            "socket_keepalive": self.socket_keepalive,
            "retry_on_timeout": self.retry_on_timeout,
            "decode_responses": self.decode_responses,
        }

        if self.password:
            config["password"] = self.password

        return config
